<?php

defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/MY_Convlatlng.php';

class Device extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->output->set_header('Access-Control-Allow-Origin: *');
    }

    // 获取设备的经纬度
    public function deviceTrack_get()
    {
        $this->load->library('MY_Convlatlng', NULL, 'convlatlng');
        $deviceSn = $this->get('deviceSn');
        $from = $this->get('from');
        $to = $this->get('to');
        if ($from > $to) {
            $temp = $to;
            $to = $from;
            $from = $temp;
        }
        $this->load->model('Device_model', 'device');
        $data = $this->device->getDeviceTrack($deviceSn, $from, $to);
        if ($data) {
            $result_array = array();
            foreach ($data as $value) {
                $convedArray = $this->convlatlng->conv($value->longitude, $value->latitude);
                array_push($result_array, $convedArray);
            }
            $this->response($result_array, REST_Controller::HTTP_OK);
        } else {
            $this->response([
                'status' => FALSE,
                'message' => 'No track were found'
            ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
        }
    }

    public function findDevice_get()
    {
        $deviceSn = $this->get('deviceSn');
        exec("php tcpClient.php", $file);
        var_dump($file);
    }
}